package filter;

import users.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter("/LoginSuccess.jsp")
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) req;
        HttpServletResponse httpServletResponse = (HttpServletResponse) resp;
        httpServletRequest.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("html/txt;charset=UTF-8");
        HttpSession session = httpServletRequest.getSession();
        User user = (User) session.getAttribute("user");
        //判断u是否为空
        if (user == null) {
            System.out.printf("未输入");
            httpServletResponse.sendRedirect("Login.jsp");
        }
        else {
            System.out.printf("已输入");
            chain.doFilter(httpServletRequest,httpServletResponse);
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
